﻿using QRCoder;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace AutoEditUI
{
    internal class Utils
    {
        public static double MBXUtilsType = 3.7808;
        public static System.Windows.Media.DrawingImage WorkForSVG(string text, double MBLConfigureQRWH)
        {
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(text, QRCodeGenerator.ECCLevel.H);
            XamlQRCode qrCode = new XamlQRCode(qrCodeData);
            System.Windows.Size size = new System.Windows.Size((int)MBLConfigureQRWH * MBXUtilsType, (int)MBLConfigureQRWH * MBXUtilsType);
            System.Windows.Media.DrawingImage qrCodeAsXaml = qrCode.GetGraphic(size, false);
            return qrCodeAsXaml;
        }
        //public static System.Windows.Media.DrawingImage WorkForUPCA(string text, double w, double h)
        //{
        //    QRCodeGenerator qrGenerator = new QRCodeGenerator();
        //    QRCodeData qrCodeData = qrGenerator.CreateQrCode(text, QRCodeGenerator.ECCLevel.H);
        //    XamlQRCode qrCode = new XamlQRCode(qrCodeData);
        //    System.Windows.Size size = new System.Windows.Size((int)(w * MBXUtilsType), (int)(h * MBXUtilsType));
        //    System.Windows.Media.DrawingImage qrCodeAsXaml = qrCode.GetGraphic(size, false);
        //    return qrCodeAsXaml;
        //}

        public static BitmapImage BitmapToBitmapImage(Bitmap bitmap)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                bitmap.Save(stream, ImageFormat.Png);
                stream.Position = 0;
                BitmapImage result = new BitmapImage();
                result.BeginInit();
                result.CacheOption = BitmapCacheOption.OnLoad;
                result.StreamSource = stream;
                result.EndInit();
                result.Freeze();
                return result;
            }
        }

        // BitmapImage --> Bitmap
        public static Bitmap BitmapImageToBitmap(BitmapImage bitmapImage)
        {
            using (MemoryStream outStream = new MemoryStream())
            {
                BitmapEncoder enc = new BmpBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(bitmapImage));
                enc.Save(outStream);
                Bitmap bitmap = new Bitmap(outStream);
                return new Bitmap(bitmap);
            }
        }

        public static void SetQRCodeImage(System.Windows.Media.DrawingImage mBMPDataQRDrawingImage, Canvas mainCanvas, System.Windows.Controls.Image QRImage, double MBLConfigureQRWH, double top, double left) {
            if (mBMPDataQRDrawingImage != null)
            {
                QRImage.Source = mBMPDataQRDrawingImage;
                QRImage.Width = MBLConfigureQRWH * MBXUtilsType;
                QRImage.Height = MBLConfigureQRWH * MBXUtilsType;
                ImageBrush ib = new ImageBrush();
                ib.ImageSource = QRImage.Source;
                ib.Stretch = Stretch.Uniform;
                Canvas.SetLeft(QRImage, left);
                Canvas.SetTop(QRImage, top);
                mainCanvas.Children.Add(QRImage);
            }

        }

        public static System.Windows.Media.Color GetMediaColorFromDrawingColor(System.Drawing.Color color)
        {
            return System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B);
        }

        internal static void RemoveItembyTag(UIModel gUIModel, string tag)
        {
            if (gUIModel.ItemIcons != null)
                foreach (ItemIcon itemIcon in gUIModel.ItemIcons)
                {
                    if (itemIcon.Tag == tag)
                    {
                        gUIModel.ItemIcons.Remove(itemIcon);
                        break;
                    }
                }
            if (gUIModel.ItemBarcodes != null)
                foreach (ItemBarcode itemBarcode in gUIModel.ItemBarcodes)
                {
                    if (itemBarcode.Tag == tag)
                    {
                        gUIModel.ItemBarcodes.Remove(itemBarcode);
                        break;
                    }
                }

            if (gUIModel.ItemQRs != null)
                foreach (ItemQR itemQR in gUIModel.ItemQRs)
                {
                    if (itemQR.Tag == tag)
                    {
                        gUIModel.ItemQRs.Remove(itemQR);
                        break;
                    }
                }

            if (gUIModel.ItemRects != null)
                foreach (ItemRect itemRect in gUIModel.ItemRects)
                {
                    if (itemRect.Tag == tag)
                    {
                        gUIModel.ItemRects.Remove(itemRect);
                        break;
                    }
                }

            if (gUIModel.ItemTexts != null)
                foreach (ItemText itemText in gUIModel.ItemTexts)
                {
                    if (itemText.Tag == tag)
                    {
                        gUIModel.ItemTexts.Remove(itemText);
                        break;
                    }
                }
        }
    }
}